// pages/leave-msg/leave-msg.js
var http = require('../../utils/http.js');
var config = require('../../utils/config.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    userName: '',
    userPhone: '',
    enterprise: '',
    remarks: '',
    MsgType: [],
    prodId:0,
  },

  chooseType(e) {
    if (e) {
      if (e.currentTarget.dataset.type === this.data.type) {
        return;
      }
      this.setData({
        type: e.currentTarget.dataset.type
      })
    }

  },
  getMsgType() {
    http.request({
      url: "/p/msgType/list",
      method: "GET",
      callBack: (res) => {
        this.setData({
          MsgType: res
        });
        this.MsgType = res;
      }
    })
  },
  Save() {
    let data = {
      msgTypeId: this.data.type,
      username: this.data.userName,
      phone: this.data.userPhone,
      company: this.data.enterprise,
      content: this.data.remarks,
      prodId:this.data.prodId
    }
    console.log(data);
    http.request({
      url: "/p/msg/add",
      method: "POST",
      data: data,
      callBack: (res) => {
        console.log(res);
        if (res) {
          wx.showToast({
            title: "保存成功",
            icon: 'success',
            duration: 2000,
            //显示透明蒙层，防止触摸穿透
            mask: true,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateBack({
                  delta: 1
                });
              }, 1500) //延迟时间
            }
          })
        }
        else {
          wx.showToast({
            title: "提交留言过于频繁，请稍后重试",
          })
        }
      }
    })

  },
  onInput_userName(e) {
    this.setData({
      userName: e.detail.value
    });
  },
  onInput_userPhone(e) {
    this.setData({
      userPhone: e.detail.value
    });
  },
  onInput_enterprise(e) {
    this.setData({
      enterprise: e.detail.value
    });
  },
  onInput_remarks(e) {
    this.setData({
      remarks: e.detail.value
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getMsgType();
    let phone = wx.getStorageSync('phone');
    console.log(options)
    this.setData({
      userPhone: phone,
      type: 1,
      prodId:options.prodId
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})